/*! 
 *  spcpl-0.1.0.min.js
 *  SharePoint Cross Plaform Library
 *  https://spcpl.codeplex.com/ 
 *
 *  An open source JavaScript library that enables easy cross platform programming 
 *  against all currently supported versions of SharePoint 
 * (2007, 2010, 2013, Office 365/SharePoint Online, and beyond!)
 * 
 *  Migrate with ease!
 */

/******************************************************************************
 * NOTES: 
 *        This JavaScript library (if used to create your customizations) 
 *        makes migrating between SharePoint product versions easy since you 
 *        don't have to do anything.  It still works after migration/upgrade 
 *        with no code changes required!
 *
 *        Behind the scenes, the library auto-detects which current SharePoint 
 *        product version you are running on and uses the appropriate 
 *        client side SharePoint API when needed.
 *
 *        It's written with TypeScript (http://www.typescriptlang.org/) and 
 *        Visual Studio 2013 to provide strong type checking at compile time 
 *        and IntelliSense at design time.
 * 
 *        There are no other dependencies other than it must run on a 
 *        SharePoint (2007, 2010, 2013, Office 365/SharePoint Online, 
 *        and beyond!) web page (aspx).
 *
 *        In addition to content editor Web Parts, master pages and 
 *        SharePoint Designer, you can also embed and use this library in 
 *        custom Web Parts, App Parts, apps for SharePoint, cloud app model, 
 *        Visual Studo, sandboxes solutions, farm solutions, and any other 
 *        SharePoint customization that has a UI component to it.
 *        
 *        This library is also intelligent in that if a version of this 
 *        library has already been loaded on the page, it will detect this, 
 *        check the version loaded, and if the detected version is lower than 
 *        this version, it will override that version with the newer 
 *        version.  Doing this detection protects against multiple versions 
 *        of the library loaded in SharePoint at the same time.
 *        
 ******************************************************************************/

interface SharePointCrossPlatformLibraryStaticFieldsAndFunctions {
    version: string;
}

(function (window, undefined?) {
    "use strict";

    var THIS_LIBRARY_VERSION_STRING = "0.1.0";

    //#region determine if shouldImplementLibrary
    // immediately do detection to see if the library has already been loaded
    var shouldImplementLibrary = true;

    // see if the spcpl global namespace exists and if the version property exists
    if (window.spcpl && window.spcpl.version) {
        // it appears that the SharePoint Cross Platform JavaScript library has already been loaded on this page
        // see if the version number is higher or lower than this current version number.

        function convertVersionStringToNumber(versionStringToConvert) {
            var returnValue = 0;
            if (versionStringToConvert !== undefined && typeof versionStringToConvert === "string" && versionStringToConvert.length > 0 && versionStringToConvert.indexOf(".") > -1) {
                var peices = versionStringToConvert.split(".");
                if (peices.length === 3) {
                    for (var i = 0; i < peices.length; i++) {
                        returnValue = returnValue + (parseInt(peices[i]) * Math.pow(1000, i));
                    }
                }
            }

            return returnValue;
        }

        var detectedVersionNumberForCompare = convertVersionStringToNumber(window.spcpl.version);
        var thisLibraryVersionNumberForCompare = convertVersionStringToNumber(THIS_LIBRARY_VERSION_STRING);
        shouldImplementLibrary = detectedVersionNumberForCompare < thisLibraryVersionNumberForCompare;
    }
    //#endregion

    if (shouldImplementLibrary) {
        // we will now implement the library!

        //#region cache DOM objects as local variables
        var location = window.location;
        var document = window.document;
        var documentElement = window.documentElement;
        //#endregion

        //#region setup namespaces
        // setup namespaces
        var spcplNamespace: SharePointCrossPlatformLibraryStaticFieldsAndFunctions = window.spcpl ? window.spcpl : {};
        //#endregion

        //#region PUBLIC STATIC FIELD: spcpl.version
        spcplNamespace.version = THIS_LIBRARY_VERSION_STRING;
        //#endregion

        //#region write namespaces back to global variables
        window.spcpl = spcplNamespace;
        //#endregion
    }

})(window);